package xyz.yolin.controller;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import xyz.yolin.pojo.Admin;
import xyz.yolin.pojo.AnswerOpt;
import xyz.yolin.service.AdminService;

import xyz.yolin.utils.MD5Utils;
import xyz.yolin.utils.MapControl;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    @RequestMapping("add")
    public String toCreate(){
        return "admin/add";
    }
    @RequestMapping("/create")
    @ResponseBody
    public Map<String,Object> create(@RequestBody Admin admin) {
        System.out.println(admin);
//        Admin a=new Admin(null,"admin","666666","系统操作员","sds");
        admin.setPassword(MD5Utils.getMD5(admin.getPassword()));
        int result=adminService.create(admin);
        if (result<=0){
            return MapControl.getInstance().err().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }


    @ResponseBody
    @PostMapping("/delete")
    public Map<String,Object> delete(String str){
        System.out.println(str);
        int result = adminService.delete(str);
        if (result<=0){
            return MapControl.getInstance().err().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(@RequestBody Admin admin){
        int result = adminService.update(admin);
        if (result<=0){
            return MapControl.getInstance().err().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    //测试方法
//    @PostMapping("/query")
//    @ResponseBody
//    public List<Admin> query(Admin admin){
//        for(Admin admin1:adminService.query(admin)){
//            System.out.println(admin1.getAccount());
//        }
//        return adminService.query(admin);
//    }

    @PostMapping("/query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Admin admin){
        System.out.println("执行query请求");
        System.out.println(admin);
        PageHelper.startPage(admin.getPage(),10);
        System.out.println("getPage:"+admin.getPage());
        System.out.println("getLimit:"+admin.getLimit());
//        System.out.println(admin.getAccount());
        if (LoginController.currUser!=1){
            admin.setId(LoginController.currUser);
            System.out.println(admin);
            List<Admin> list = adminService.query(admin);
            Integer count=adminService.count(admin);
            Map<String, Object> map = MapControl.getInstance().page(list, count).getMap();
            System.out.println(map);
            return map;
        }
        List<Admin> list = adminService.query(admin);
        Integer count=adminService.count(admin);
        Map<String, Object> map = MapControl.getInstance().page(list, count).getMap();
        System.out.println(map);
        return map;
    }
    
    @RequestMapping("/detail")
    public String detail(Integer id, ModelMap modelMap){
//       return adminService.detail(id);
        Admin admin = adminService.detail(id);
        System.out.println("根据id查的admin:"+admin);
        modelMap.addAttribute("admin",admin);
        return "admin/update";
    }

    @GetMapping("/list")
    public String list(){
        return "admin/list";
    }
}
